﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPublisher.Web.Framework;
using VietPublisher.BSL.BK;
using VietPublisher.BaseProject.Models;
using VietPublisher.FileManager;


namespace VietPublisher.Web.BK.Controllers.Admin
{
    public class AuthorController : StringBaseEntityController
    {
        //
        // GET: /Author/
       // [CustomAuthorize(Roles = SiteRoles.Customer)]
         [CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult Index()
        {
            return View();
        }

     //   [Authorize(Roles = "Admin")]
          [CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult AuthorList()
        {
            List<T_BK_Author> authors = RepoAuthor.GetAuthorList();
            return View("List_Author", authors);
        }

        //
        // Get : /Author
        [HttpGet,   CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult CreateAuthor()
        {
            return View();
        }



        /// <summary>
        /// Use this method when
        /// 1. Add author when search author in popup of create book
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost, CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult DoCreateAuthor(FormCollection collection)
        {
            int result = 0;
            try
            {
                string authorName = Request.Form["authorName"];
                T_BK_Author author = new T_BK_Author()
                {
                    AuthorName = authorName
                };
              result = RepoAuthor.AddAuthor(author);
            }
            catch (Exception e)
            {
                LogUtility.LogMessage("AuthorController","DoCreateAuthor",e.Message);
                return Content(result.ToString());
            }
            return Content(result.ToString());
        }


        //
        // Get : /Author
        [HttpGet, CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult UpdateAuthor()
        {
            return View();
        }


        //
        // Post : /Author
        [HttpPost, CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult DoUpdateAuthor(FormCollection collection)
        {
            return View();
        }


        //
        // Get : /Author
        [HttpGet, CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult DeleteAuthor(int authorID)
        {
            return View();
        }

        /// <summary>
        /// call popup when modify book, this popup can be add author to group
        /// or creat new author 
        /// </summary>
        /// <returns></returns>
        [CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult OpenAuthorPopup()
        {
            var allAuthor= RepoAuthor.GetAuthorList();
            //step 1: get param authorNames from book to modify in this (Right div)
           
            string authorNames = Request.Params["authorNames"] != null ? Request.Params["authorNames"].ToString() : "";
            List<int> authorArr = new List<int>();
            foreach(var i in authorNames.Split(',')){
                if(string.IsNullOrEmpty(i)){
                    continue;
                }
                authorArr.Add(Int32.Parse(i));
            }
            //--------------
            ViewData["authorSelected"]= allAuthor.Where(m=> authorArr.Contains(m.AuthorID)).ToList();

            //step 2: get all author to left div
            ViewData["Author"] = allAuthor;
            return View();
        }

        /// <summary>
        /// search list author with input param is author name
        /// ex: fillter list of author when choose author for a book in author popup
        /// </summary>
        /// <param name="AuthorName"></param>
        /// <returns></returns>
        [CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult SearchAuthorList(string authorName)
        {
            authorName = HttpUtility.UrlDecode(authorName);
            ViewData["Author"] = RepoAuthor.GetAuthorList(authorName);
            return PartialView("_LeftInPopupAuthor");
        }

        [HttpPost, CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult DoCreateAuthorGroup(FormCollection collection)
        {
            return View();
        }

        /// <summary>
        /// 1.write session about author when add author from author popup to create book
        /// 2.Book controller use this session to save author group
        /// 3.Session["authorIds"]
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        //[HttpPost]
        //public ActionResult SessionAuthorSave(FormCollection collection)
        //{
        //    string AuthorIds = Request.Form["AuthorIds"] != null ? Request.Form["AuthorIds"].ToString() : string.Empty;
        //    List<int> AuthorIdsArr = new List<int>();
        //    foreach (var i in AuthorIds.Split(','))
        //    {
        //        if (string.IsNullOrEmpty(i))
        //        {
        //            continue;
        //        }
        //        AuthorIdsArr.Add(Int32.Parse(i));
        //    }
        //    Session["authorIds"] = AuthorIdsArr;

        //    return Content(string.Empty);
        //}

    }
}
